package com.lc.rongzecaifu.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lc.rongzecaifu.MyApplication;
import com.lc.rongzecaifu.conn.JsonAccessToken;
import com.lc.rongzecaifu.conn.JsonFindpass;
import com.lc.rongzecaifu.conn.JsonReg;
import com.lc.rongzecaifu.conn.JsonSendcode;
import com.lc.rongzecaifu.rongzecaifu.R;
import com.lc.rongzecaifu.toast.LovelyToast;
import com.lc.rongzecaifu.ui.RegistWebActivity;
import com.lc.rongzecaifu.view.ClearEditText;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilMatches;

import butterknife.Bind;
import butterknife.ButterKnife;


public class RegisterActivity extends BaseActivity2 implements View.OnClickListener {
    @Bind(R.id.et_01)
    ClearEditText et01;
    @Bind(R.id.et_02)
    EditText et02;
    @Bind(R.id.bt_getcode)
    TextView btGetcode;
    @Bind(R.id.et_03)
    ClearEditText et03;
    @Bind(R.id.et_04)
    ClearEditText et04;
    @Bind(R.id.et_05)
    ClearEditText et05;
    @Bind(R.id.bt_registered)
    Button btRegistered;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.iv_back)
    ImageView ivBack;
    @Bind(R.id.recommend_ll)
    LinearLayout recommend_ll;
    @Bind(R.id.regist_bg_ll)
    LinearLayout regist_bg_ll;

    private CountDownTimer countTime = new CountDownTimer(61000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {

            if (!isFinishing()) {

                btGetcode.setText(millisUntilFinished / 1000 + "s重新获取");

            }
        }

        @Override
        public void onFinish() {

            if (!isFinishing()) {
                btGetcode.setText("重新获取");
                btGetcode.setEnabled(true);
            }
        }
    };

    private boolean isRegistMode = true;//true 注册模式；false 找回密码
    private String codeId = "";
    private String codeMode = "reg";//默认为空，发普通短信 reg:注册短信 findpass:找回密码

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register2);
        ButterKnife.bind(this);
        if ((getIntent().getIntExtra(LoginActivity.FLAG, 0)) == 2) {
            isRegistMode = false;
            codeMode = "findpass";
            tvTitle.setText(getResources().getText(R.string.register_title_forgot));

            btRegistered.setText("确定");

            regist_bg_ll.setBackgroundResource(R.mipmap.forget_bg);

            recommend_ll.setVisibility(View.GONE);

        }

        btGetcode.setOnClickListener(this);
        btRegistered.setOnClickListener(this);
        btRegistered.setSelected(false);

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.bt_getcode:

                sentCode();

                break;

            case R.id.bt_registered:
                if (btRegistered.isSelected()) {
                    if (isRegistMode) {
                        onRegist();
                    } else {
                        onResetPW();
                    }
                } else if (et01.getText().toString().equals("")) {
                    //  UtilToast.show(RegisterActivity.this, "请输入手机号");
                    LovelyToast.makeText(RegisterActivity.this, "请输入手机号", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);

                } else if (!UtilMatches.checkMobile(et01.getText().toString())) {
                    LovelyToast.makeText(RegisterActivity.this, "请输入正确的手机号", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                    // UtilToast.show(RegisterActivity.this, "请输入正确的手机号");
                } else {
                    //   UtilToast.show(RegisterActivity.this, "请点击按钮获取验证码");
                    LovelyToast.makeText(RegisterActivity.this, "请点击按钮获取验证码", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                }


                break;
        }
    }

    private void sentCode() {
        if (et01.getText().toString().equals("")) {

            //UtilToast.show(this, "请输入手机号");
            LovelyToast.makeText(RegisterActivity.this, "请输入手机号", 0, LovelyToast
                    .SUCCESS, LovelyToast.SCALE);
        } else if (!UtilMatches.checkMobile(et01.getText().toString())) {

            //  UtilToast.show(this, "请输入正确的手机号");
            LovelyToast.makeText(RegisterActivity.this, "请输入正确的手机号", 0, LovelyToast
                    .SUCCESS, LovelyToast.SCALE);
        } else {

           btGetcode.setEnabled(false);

            getSendCodeAccessToken();

        }

    }

    private void getSendCodeAccessToken() {


        new JsonAccessToken("common/sendcode", new AsyCallBack<JsonAccessToken.Info>() {

            @Override
            public void onStart(int type) throws Exception {
                super.onStart(type);
                showLoading();
            }

            @Override
            public void onSuccess(String toast, int type, JsonAccessToken.Info info) throws Exception {

                initSendCode(info.access_token);

            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
                //   UtilToast.show(RegisterActivity.this, "安全验证失败！");
                LovelyToast.makeText(RegisterActivity.this, "安全验证失败", 0, LovelyToast
                        .SUCCESS, LovelyToast.SCALE);
                btGetcode.setEnabled(true);
                btGetcode.setText("获取验证码");
                dismissLoading();
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
                dismissLoading();
            }
        }).execute(this, true);



    }


    private void initSendCode(String access_token) {
        new JsonSendcode(access_token, et01.getText().toString(), codeMode, new AsyCallBack<JsonSendcode.Info>() {

            @Override
            public void onSuccess(String toast, int type, JsonSendcode.Info info) throws Exception {
                if (info != null) {
                    codeId = info.codeId;
                    btRegistered.setSelected(true);
                    countTime.start();
                }
            }

            @Override
            public void onFail(String toast, int type) throws Exception {

                btGetcode.setEnabled(true);
//                btGetcode.setText("获取验证码");
                dismissLoading();

            }

            @Override
            public void onEnd(String toast, int type) throws Exception {

                // UtilToast.show(RegisterActivity.this, JsonSendcode.TOAST);
                LovelyToast.makeText(RegisterActivity.this, JsonSendcode.TOAST, 0, LovelyToast
                        .SUCCESS, LovelyToast.SCALE);
            }

        }).execute(this, true);
    }

    private void onResetPW() {
        if (TextUtils.isEmpty(et01.getText().toString().trim())) {


            LovelyToast.makeText(RegisterActivity.this, "手机号不能为空", 0, LovelyToast
                    .SUCCESS, LovelyToast.SCALE);
        } else if (!UtilMatches.checkMobile(et01.getText().toString())) {
            LovelyToast.makeText(RegisterActivity.this, "请输入正确的手机号", 0, LovelyToast
                    .SUCCESS, LovelyToast.SCALE);

        } else if (TextUtils.isEmpty(codeId)) {

            LovelyToast.makeText(RegisterActivity.this, "请点击获取验证码", 0, LovelyToast
                    .SUCCESS, LovelyToast.SCALE);
        } else if (et02.getText().toString().equals("")) {

            // UtilToast.show(this, "请输入验证码");
            LovelyToast.makeText(RegisterActivity.this, "请输入验证码", 0, LovelyToast
                    .SUCCESS, LovelyToast.SCALE);
        } else if (TextUtils.isEmpty(et03.getText().toString().trim())) {
            LovelyToast.makeText(RegisterActivity.this, "密码不能为空", 0, LovelyToast
                    .SUCCESS, LovelyToast.SCALE);
        } else if (et03.getText().toString().trim().length() < 6 || et03.getText().toString().trim().length() > 15) {

            //   UtilToast.show(this, et03.getHint());
            LovelyToast.makeText(RegisterActivity.this, et03.getHint().toString(), 0, LovelyToast
                    .SUCCESS, LovelyToast.SCALE);
        } else if (et03.getText().toString().contains(" ")) {
            LovelyToast.makeText(RegisterActivity.this, "密码不能包含空格", 0, LovelyToast
                    .SUCCESS, LovelyToast.SCALE);
            // UtilToast.show(this, "密码不能包含空格");

        } else if (!et03.getText().toString().equals(et04.getText().toString())) {

            //  UtilToast.show(this, "两次密码输入不一致");
            LovelyToast.makeText(RegisterActivity.this, "两次密码输入不一致", 0, LovelyToast
                    .SUCCESS, LovelyToast.SCALE);
        } else {

            getFindPassAccessToken();

        }
    }

    private void getFindPassAccessToken() {
        new JsonAccessToken("common/findpass", new AsyCallBack<JsonAccessToken.Info>() {
            @Override
            public void onSuccess(String toast, int type, JsonAccessToken.Info info) throws Exception {
                if (info != null) {
                    if (!TextUtils.isEmpty(info.access_token)) {
                        initFindPass(info.access_token);
                    } else {
                        // UtilToast.show(RegisterActivity.this, "安全验证失败！");
                        LovelyToast.makeText(RegisterActivity.this, "安全验证失败", 0, LovelyToast
                                .SUCCESS, LovelyToast.SCALE);
                    }
                }
            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
                // UtilToast.show(RegisterActivity.this, "安全验证失败！");
                LovelyToast.makeText(RegisterActivity.this, "安全验证失败", 0, LovelyToast
                        .SUCCESS, LovelyToast.SCALE);
                dismissLoading();
            }
        }).execute(this, true);

    }


    private void initFindPass(String access_token) {
        new JsonFindpass(access_token, et01.getText().toString(), et02.getText().toString(), codeId, et03.getText().toString(), new AsyCallBack() {
            @Override
            public void onSuccess(String toast, int type, Object o) throws Exception {
                if (o.equals("")) {
                    finish();
                }
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                //   UtilToast.show(RegisterActivity.this, JsonFindpass.TOAST);
                LovelyToast.makeText(RegisterActivity.this, JsonFindpass.TOAST, 0, LovelyToast
                        .SUCCESS, LovelyToast.SCALE);
            }

        }).execute(this, true);
    }


    public void onRegist() {

        if (TextUtils.isEmpty(et01.getText().toString().trim())) {

            LovelyToast.makeText(RegisterActivity.this, "手机号不能为空", 0, LovelyToast
                    .SUCCESS, LovelyToast.SCALE);
        } else if (!UtilMatches.checkMobile(et01.getText().toString())) {

            //UtilToast.show(this, "请输入正确的手机号");
            LovelyToast.makeText(RegisterActivity.this, "请输入正确的手机号", 0, LovelyToast
                    .SUCCESS, LovelyToast.SCALE);
        } else if (TextUtils.isEmpty(codeId)) {

            // UtilToast.show(this, "请点击获取验证码");
            LovelyToast.makeText(RegisterActivity.this, "请点击获取验证码", 0, LovelyToast
                    .SUCCESS, LovelyToast.SCALE);
        } else if (TextUtils.isEmpty(et02.getText().toString().trim())) {

            LovelyToast.makeText(RegisterActivity.this, "验证码不能为空", 0, LovelyToast
                    .SUCCESS, LovelyToast.SCALE);
        } else if (TextUtils.isEmpty(et03.getText().toString().trim())) {
            LovelyToast.makeText(RegisterActivity.this, "密码不能为空", 0, LovelyToast
                    .SUCCESS, LovelyToast.SCALE);
        } else if (et03.getText().toString().length() < 6 || et03.getText().toString().length() > 15) {

            LovelyToast.makeText(RegisterActivity.this, et03.getHint().toString(), 0, LovelyToast
                    .SUCCESS, LovelyToast.SCALE);
        } else if (TextUtils.isEmpty(et04.getText().toString().trim())) {
            LovelyToast.makeText(RegisterActivity.this, "确认密码不能为空", 0, LovelyToast
                    .SUCCESS, LovelyToast.SCALE);
        } else if (!et03.getText().toString().trim().equals(et04.getText().toString().trim())) {

            LovelyToast.makeText(RegisterActivity.this, "两次密码输入不一致", 0, LovelyToast
                    .SUCCESS, LovelyToast.SCALE);
        } else {

            getAccessToken();

        }
    }

    private void getAccessToken() {
        new JsonAccessToken("common/reg", new AsyCallBack<JsonAccessToken.Info>() {
            @Override
            public void onSuccess(String toast, int type, JsonAccessToken.Info info) throws Exception {

                initReg(info.access_token);

            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                //UtilToast.show(RegisterActivity.this, "安全验证失败！");
                LovelyToast.makeText(RegisterActivity.this, "安全验证失败！", 0, LovelyToast
                        .SUCCESS, LovelyToast.SCALE);
                dismissLoading();
            }

        }).execute(this, true);

    }


    private void initReg(String access_token) {

        new JsonReg(access_token, et01.getText().toString(), et03.getText().toString(), et02.getText().toString(), codeId, et05.getText().toString(), new AsyCallBack<JsonReg.RegistBackInfo>() {
            @Override
            public void onSuccess(String toast, int type, JsonReg.RegistBackInfo info) throws Exception {

                MyApplication.myPreferences.saveUid(info.uid);

                startActivity(new Intent(context, RegistWebActivity.class).putExtra("url", info.url));

                MyApplication.INSTANCE.finishActivity(RegisterActivity.class, LoginActivity.class);

            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                // UtilToast.show(RegisterActivity.this, JsonReg.TOAST);
                LovelyToast.makeText(RegisterActivity.this, JsonReg.TOAST, 0, LovelyToast
                        .SUCCESS, LovelyToast.SCALE);
            }

        }).execute(this, true);
    }
}
